package com.jquest.editor.components;
import com.jquest.misc.Tileset;
import org.newdawn.slick.*;

import java.awt.*;
import java.awt.Font;
import java.util.HashMap;
import javax.swing.*;
/*
 * Created by JFormDesigner on Thu Dec 17 05:26:13 CET 2009
 */



/**
 * @author Johan Ljungberg
 */
public class SidePanel extends JPanel {
   //ToDo: Code cleanup, bind these visual components to the editor itself, fix UI to allow smooth resizing
   public Tileset ts;

    DefaultListModel tileModel;
    DefaultListModel layerModel;
    
	public SidePanel() {
		initComponents();
        this.setPreferredSize(new Dimension(200, 800));
	}

    public void reloadTileset() {
        HashMap<String, org.newdawn.slick.Image> imgs = ts.getTiles();
        tileModel.clear();
        for(org.newdawn.slick.Image img : imgs.values()) {
            tileModel.addElement(img);
        }
        imageList.setModel(tileModel);
        //imageList.ensureIndexIsVisible(0);
        imageScroll.revalidate();
        imageScroll.repaint();
    }

    public void assignTileset(Tileset ts) {
        this.ts = ts;
        reloadTileset();
    }

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		label1 = new JLabel();
		tilesetPanel = new JPanel();
		imageScroll = new JScrollPane();
        tileModel = new DefaultListModel();
		imageList = new ImageList(tileModel);
		changeTileset = new JButton();
		label2 = new JLabel();
		layerPanel = new JPanel();
		layerScroll = new JScrollPane();
		layerList = new JList();
		addLayer = new JButton();
		removeLayer = new JButton();

        layerModel = new DefaultListModel();

        layerList.setModel(layerModel);
        
		//======== this ========
		setLayout(new FlowLayout());

		//---- label1 ----
		label1.setText("Tileset");
		label1.setFont(new Font("Tahoma", Font.PLAIN, 14));
		add(label1);

		//======== tilesetPanel ========
		{
			tilesetPanel.setLayout(new BorderLayout());

			//======== imageScroll ========
			{

				//---- imageList ----
				imageScroll.setViewportView(imageList);
                imageScroll.setPreferredSize(new Dimension(200, 280));

			}
			tilesetPanel.add(imageScroll, BorderLayout.CENTER);

			//---- changeTileset ----
			changeTileset.setText("Change Tileset");
			tilesetPanel.add(changeTileset, BorderLayout.SOUTH);
		}
		add(tilesetPanel);

		//---- label2 ----
		label2.setText("Layers");
		label2.setFont(new Font("Tahoma", Font.PLAIN, 14));
		add(label2);

		//======== layerPanel ========
		{
			layerPanel.setLayout(new BorderLayout());

			//======== layerScroll ========
			{

				//---- layerList ----
				//layerList.setPreferredSize(new Dimension(300, 50));
				layerScroll.setViewportView(layerList);
                layerScroll.setPreferredSize(new Dimension(200, 100));
			}
			layerPanel.add(layerScroll, BorderLayout.CENTER);
		}
		add(layerPanel);

		//---- addLayer ----
		addLayer.setText("+");
		add(addLayer);

		//---- removeLayer ----
		removeLayer.setText("-");
		add(removeLayer);

		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	private JLabel label1;
	private JPanel tilesetPanel;
	private JScrollPane imageScroll;
	public ImageList imageList;
	private JButton changeTileset;
	private JLabel label2;
	private JPanel layerPanel;
	private JScrollPane layerScroll;
	private JList layerList;
	private JButton addLayer;
	private JButton removeLayer;
	// JFormDesigner - End of variables declaration  //GEN-END:variables
}
